package demo.practice.linklist;

import demo.practice.ListNode;

import java.util.HashSet;
import java.util.Set;

public class P83 {


    public static void main(String[] args) {
        P83 p82= new P83();

        ListNode head= new ListNode(1);
        head.next= new ListNode(2);
        head.next.next= new ListNode(3);
        head.next.next.next= new ListNode(3);
        head.next.next.next.next= new ListNode(4);
        head.next.next.next.next.next= new ListNode(4);
        head.next.next.next.next.next.next= new ListNode(5);

        p82.deleteDuplicates(head);

    }

    public ListNode deleteDuplicates(ListNode head) {

        if(head==null)
            return head;

        Set<Integer> set= new HashSet<>();
        
        ListNode cur= head;
        ListNode pre= null;

        while (cur!=null){
            boolean a = set.add(cur.val);
            if(!a){
                //重复的情况
                pre.next=cur.next;
                cur=cur.next;
            }else{
                pre= cur;
                cur=cur.next;

            }
        }

        return head;

    }
}
